package com.tcfvic.server;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tcfvic.client.IndustryDetailsListService;
import com.tcfvic.client.panels.widgets.addons.advancedtable.DataFilter;
import com.tcfvic.client.panels.widgets.addons.advancedtable.TableColumn;
import com.tcfvic.client.pojos.Industry;
import com.tcfvic.server.persistance.HibernateUtil;

public class IndustryDetailsListServiceImpl extends RemoteServiceServlet
		implements IndustryDetailsListService {

	private static final long serialVersionUID = 7477831912323588569L;
	private static final Logger logger = Logger
			.getLogger(IndustryDetailsListServiceImpl.class);

	@Override
	public TableColumn[] getColumns() {
		TableColumn[] tc = new TableColumn[2];
		try {
			tc[0] = new TableColumn("id", "id");
			tc[1] = new TableColumn("name", "Industry name");
		} catch (Exception e) {
			logger.error("Error getting column names for industry list", e);
		}
		return tc;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String[][] getRows(int startRow, int rowsCount,
			DataFilter[] filters, String sortColumn, boolean sortOrder) {

		String[][] data = null;
		try {
			Session session = HibernateUtil.getSession();

			session.beginTransaction();
			List<Industry> industries = new ArrayList<Industry>();

			Criteria crit = session.createCriteria(Industry.class);
			crit.add(Restrictions.or(Restrictions.isNull("display"),
					Restrictions.eq("display", "")));
			crit.setFirstResult(startRow);
			crit.setMaxResults(rowsCount);
			if (!sortOrder) {
				crit.addOrder(Order.asc("name"));
			} else {
				crit.addOrder(Order.desc("name"));
			}
			if (filters != null) {
				for (DataFilter df : filters) {
					if (df != null && !df.getValue().isEmpty()) {
						crit.add(Restrictions.like(df.getColumn(), df
								.getValue()
								+ "%"));
					}
				}
			}
			industries.addAll(crit.list());

			data = new String[industries.size()][4];
			int i = 0;
			for (Industry industry : industries) {
				data[i][0] = String.valueOf(industry.getId());
				data[i][1] = industry.getName();
				i++;
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			System.err.println(e);
		}

		return data;
	}

	@Override
	public int getRowsCount(DataFilter[] filters) {
		int size = 0;
		try {
			Session session = HibernateUtil.getSession();

			session.beginTransaction();
			Criteria crit = session.createCriteria(Industry.class);
			crit.add(Restrictions.or(Restrictions.isNull("display"),
					Restrictions.eq("display", "")));
			if (filters != null) {
				for (DataFilter df : filters) {
					if (df != null && !df.getValue().isEmpty()) {
						crit.add(Restrictions.like(df.getColumn(), df
								.getValue()
								+ "%"));
					}
				}
			}
			size = crit.list().size();

			session.getTransaction().commit();
		} catch (Exception e) {
			System.err.println(e);
		}
		return size;
	}

}
